package com.example.asus.drawstudy.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.asus.drawstudy.R;

/**
 * Created by liangkun on 2018/7/16.
 * 作用:
 */

public class CicrleProgressView extends View {
    // 画实心圆的画笔
    private Paint mCirclePaint;
    // 画圆环的画笔
    private Paint mRingPaint;
    // 画圆环的画笔背景色
    private Paint mRingPaintBg;
    // 画字体的画笔
    private Paint mTextPaint;
    // 圆环颜色
    private int mRingColor;
    // 圆环背景颜色
    private int mRingBgColor;
    // 半径
//    private float mRadius;

    // 背景圆形半径
    private float mBgRadius;

    // 圆环半径
//    private float mRingRadius;
    // 圆环宽度
    private float mStrokeWidth;
    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;
    // 字的长度
    private float mTxtWidth;
    // 字的长度
    private float mTxtDesWidth;
    // 字的高度
    private float mTxtHeight;
    // 总进度
    private int mTotalProgress = 100;
    // 当前进度
    private int mProgress;
    private Paint paintDes;
    private Paint paintPersent;
    private float persentWidth;

    public CicrleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 获取自定义的属性
        initAttrs(context, attrs);
        initVariable(context);
    }

    //属性
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TasksCompletedView, 0, 0);
//        mBgRadius = typeArray.getDimension(R.styleable.TasksCompletedView_ringBgRadius, 80);
        mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_strokeWidth, 10);
        mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF);
        mRingBgColor = typeArray.getColor(R.styleable.TasksCompletedView_ringBgColor, 0xFFFFFFFF);


    }

    //初始化画笔
    private void initVariable(Context context) {
        //内圆
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(Color.TRANSPARENT);
        mCirclePaint.setStyle(Paint.Style.FILL);

        //外圆弧背景
        mRingPaintBg = new Paint();
        mRingPaintBg.setAntiAlias(true);
        mRingPaintBg.setColor(mRingBgColor);
        mRingPaintBg.setStyle(Paint.Style.STROKE);
        mRingPaintBg.setStrokeWidth(mStrokeWidth);


        //外圆弧
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);
        mRingPaint.setStrokeCap(Paint.Cap.ROUND);//设置线冒样式，有圆 有方

        //中间字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);

        //中间字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);

        paintPersent = new Paint();
        paintPersent.setAntiAlias(true);
        paintPersent.setStyle(Paint.Style.FILL);
        paintPersent.setColor(mRingColor);
        paintPersent.setTextSize(80);

        //字体
        Typeface font = Typeface.createFromAsset(context.getAssets(), "DINOT-CONDMEDIUM.OTF");
        mTextPaint.setTextSize(160);
        mTextPaint.setTypeface(font);
        //文字电量
        //绘制描述颜色
        paintDes = new Paint();
        paintDes.setColor(Color.WHITE);
        paintDes.setAlpha(150);
        paintDes.setTextSize(40);

        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);

    }

    //画图
    @Override
    protected void onDraw(Canvas canvas) {
        mXCenter = getWidth() / 2;
        mYCenter = getHeight() / 2;
        Log.e("kkk","width "+getWidth()+" height "+getHeight());
        //内圆
        mBgRadius = mXCenter - mStrokeWidth*3;
        canvas.drawCircle(mXCenter, mYCenter, mBgRadius, mCirclePaint);

        //外圆弧背景
        RectF oval1 = new RectF();
        oval1.left = (mXCenter - mBgRadius);
        oval1.top = (mYCenter - mBgRadius);
        oval1.right = mBgRadius * 2 + (mXCenter - mBgRadius);
        oval1.bottom = mBgRadius * 2 + (mYCenter - mBgRadius);
        canvas.drawArc(oval1, 0, 360, false, mRingPaintBg); //圆弧所在的椭圆对象、圆弧的起始角度、圆弧的角度、是否显示半径连线

        //外圆弧
        if (mProgress > 0) {
            RectF oval = new RectF();
            oval.left = (mXCenter - mBgRadius);
            oval.top = (mYCenter - mBgRadius);
            oval.right = mBgRadius * 2 + (mXCenter - mBgRadius);
            oval.bottom = mBgRadius * 2 + (mYCenter - mBgRadius);
            canvas.drawArc(oval, -90, -((float) mProgress / mTotalProgress) * 360, false, mRingPaint); //

            //绘制百分比
            String txt = mProgress + "";
            mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
            canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
            String persent ="%";
            persentWidth = paintPersent.measureText(persent, 0, persent.length());
            canvas.drawText(persent, mXCenter +mTxtWidth / 2, mYCenter + mTxtHeight / 4, paintPersent);

            String txtDes = "手环电量剩余";
            mTxtDesWidth = paintDes.measureText(txtDes, 0, txtDes.length());
            canvas.drawText(txtDes, mXCenter - mTxtDesWidth / 2, mYCenter + 150, paintDes);


        }
    }

    //设置进度
    public void setProgress(int progress) {
        mProgress = progress;
        postInvalidate();//重绘
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
